<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@tag pageEncoding="UTF-8" isELIgnored="false"  %>
<%@attribute name="goodsId"%>
<div id="batchManager" class="myDialog">
    <form class="layui-form" lay-filter="batchManager">
        <input type="hidden" name="batchHouseId">
        <input type="hidden" name="numberInputName">
        <a class="layui-btn layui-btn-normal addBatchBodyLine" current="3">新增行</a>
        <table class="layui-table">
            <thead>
            <tr>
                <th>序号</th>
                <th>数量</th>
                <th>批次号</th>
                <th>生产日期</th>
            </tr>
            </thead>

            <tbody id="batchBody">
            <tr>
                <td>1</td>
                <td><input type="number" name="batch_num1" class="number layui-input"></td>
                <td><input type="number" name="batch_batch1" class="batch layui-input"></td>
                <td><input type="text" name="batch_date1" id="initShelfLifeDate1" class="date layui-input"></td>
            </tr>
            <tr>
                <td>2</td>
                <td><input type="number" name="batch_num2" class="number layui-input"></td>
                <td><input type="number" name="batch_batch2" class="batch layui-input"></td>
                <td><input type="text" name="batch_date2" id="initShelfLifeDate2" class="date layui-input"></td>
            </tr>
            <tr>
                <td>3</td>
                <td><input type="number" name="batch_num3" class="number layui-input"></td>
                <td><input type="number" name="batch_batch3" class="batch layui-input"></td>
                <td><input type="text" name="batch_date3" id="initShelfLifeDate3" class="date layui-input"></td>
            </tr>
            </tbody>
        </table>

        <hr style="margin: 20px 0 10px 0">

        <div class="layui-form-item">
            <a id="batchSave" class="layui-btn">保存</a>
            <a id="batchCancel" class="layui-btn layui-btn-primary">取消</a>
        </div>
    </form>
    <script>

        layui.use(['form', 'layedit', 'laydate','element','table'], function(){
            var form = layui.form,
                laydate = layui.laydate;
            //日期
            laydate.render({
                elem: '#initShelfLifeDate1'
            });
            laydate.render({
                elem: '#initShelfLifeDate2'
            });
            laydate.render({
                elem: '#initShelfLifeDate3'
            });


            $("#batchSave").click(function () {

                var data = getFormJson($("form[lay-filter='batchManager']"));
                console.log(JSON.stringify(data));
                var current = parseInt($(".addBatchBodyLine").attr("current"));
                $(".number").css("border-color","");
                $(".batch").css("border-color","");
                $(".date").css("border-color","");

                //检查正确性
                var flag=true;
                for (var i=1; i<= current; i++) {
                    var num = data['batch_num' + i];
                    var batch = data['batch_batch' + i];
                    var date = data['batch_date' + i];
                    if (num == "" && batch =="" && date == ""){
                        //都为空则略过
                        continue;
                    } else if (num == "" || batch =="" || date == ""){
                        flag = false;
                        if (num == "") {
                            tip("数量不能为空");
                            $("[name='batch_num"+i+"']").css("border-color","red");
                        } else if(batch =="") {
                            tip("批次号不能为空");
                            $("[name='batch_batch"+i+"']").css("border-color","red");
                        } else if(date =="") {
                            tip("生产日期不能为空");
                            $("[name='batch_date"+i+"']").css("border-color","red");
                        }
                        break;
                    }
                }
                if (!flag) {
                    return;
                }

                //检测批次号是否重复
                var newArr = [];
                var repeat = false;
                for (var i=1; i<= current; i++) {
                    var num = data['batch_num' + i];
                    var batch = data['batch_batch' + i];
                    var date = data['batch_date' + i];
                    if (num != "" && batch !="" && date != "") {
                        var flag = true;
                        for (var j=0;j<newArr.length;j++) {
                            if (batch == newArr[j].batch) {
                                flag = false;
                                break;
                            }
                        }
                        if (flag) {
                            newArr.push({num:num,batch:batch,date:date,houseId:data.batchHouseId});
                        } else {
                            repeat = true;
                            tip("同一仓库下的同一批次不能重复");
                            $(".batch").each(function () {
                                if ($(this).val() == batch) {
                                    $(this).css("border-color","red")
                                }
                            });
                            break;
                        }
                    }
                }
                if (repeat) {
                    return;
                }


                var number = 0;

                shelfLifeList["'"+data.batchHouseId+"'"].length = 0;
                for (var i=1; i<= current; i++) {
                    var num = data['batch_num' + i];
                    var batch = data['batch_batch' + i];
                    var date = data['batch_date' + i];
                    if (num != "" && batch !="" && date != "") {
                        number += parseInt(num);
                        shelfLifeList["'"+data.batchHouseId+"'"].push({num:num,batch:batch,date:date,houseId:data.batchHouseId});
                    }
                }
                $("[name='"+data.numberInputName+"']").val(number);
                console.log(JSON.stringify(shelfLifeList["'"+data.batchHouseId+"'"]));
                closeAll();
            });

            $(".addBatchBodyLine").click(function () {
                var $this = $(this);
                var current = parseInt($this.attr("current"));
                current = current + 1;
                var child="<tr>";
                child += "<td>"+current+"</td>";
                child += "<td><input type='number' name='batch_num"+current+"' class='number layui-input'></td>";
                child += "<td><input type='number' name='batch_batch"+current+"' class='batch layui-input'></td>";
                child += "<td><input type='text' name='batch_date"+current+"' id='initShelfLifeDate"+current+"' class='date layui-input'></td>";
                child += "</tr>";
                $("#batchBody").append(child);

                layui.laydate.render({
                    elem: '#initShelfLifeDate'+current
                });
                formRender();
                $this.attr("current",current);
            });

            if ("${goodsId}" != "") {
                get("${baseUrl}goods/shelfLife/listByGoods.action?goodsId=${goodsId}",function (resp) {
                    if (resp.code == 0) {
                        if (resp.data.length <= 0) {
                            return;
                        }
                        $.each(resp.data,function (i,d) {
                            shelfLifeList["'"+d.houseId+"'"].push(d);
                        });
                    }
                });
            }

        });


        $("#batchCancel").click(function () {
            closeAll();
        });


    </script>
</div>